import { ipcMain } from 'electron-better-ipc'
import * as os from 'node:os'
import * as path from 'node:path'
// const homedir =

export const APP_NAME = 'AssetsTool'

// 获取完整的应用数据目录，不对外暴露，外面使用APP_DATA_DIR即可
const getAppDataDir = () => {
    const platform = process.platform
    const homedir = process.env.HOME || os.homedir()
    if (platform === 'win32') {
        const appDataDir = process.env.APPDATA || homedir
        return path.join(appDataDir, APP_NAME)
    } else if (platform === 'darwin') {
        return path.join(homedir, 'Library', 'Application Support', APP_NAME)
    } else {
        return path.join(homedir, APP_NAME)
    }
}

// 应用数据目录，获取系统的数据目录
// windows是%USERPROFILE%\AppData\Roaming\AppName
// macOS是~/Library/Application Support/AppName
export const APP_DATA_DIR = getAppDataDir()
// 打开的工作空间历史列表文件
export const WORK_SPACE_HISTORY_FILE = path.join(APP_DATA_DIR, 'workSpaceHistory.json')

// 应用配置文件目录
export const CONFIGS_FILE = path.join(APP_DATA_DIR, 'configs.json')

// 工作空间数据目录
export const getWorkSpaceDataDirById = (id: string) => {
    return path.join(APP_DATA_DIR, 'workSpace', id)
}

// 工作空间数据文件
export const getWorkSpaceDataFilePathById = (id: string) => {
    return path.join(getWorkSpaceDataDirById(id), 'data.json')
}

// 工作空间设计稿数据文件
export const getWorkSpaceDesignDataFilePathById = (id: string) => {
    return path.join(getWorkSpaceDataDirById(id), 'designData.json')
}

ipcMain.answerRenderer('APP_NAME', () => APP_NAME)
ipcMain.answerRenderer('APP_DATA_DIR', () => APP_DATA_DIR)
ipcMain.answerRenderer('WORK_SPACE_HISTORY_FILE', () => WORK_SPACE_HISTORY_FILE)
ipcMain.answerRenderer('CONFIGS_FILE', () => CONFIGS_FILE)
ipcMain.answerRenderer('getWorkSpaceDataDirById', (id: string) => getWorkSpaceDataDirById(id))
ipcMain.answerRenderer('getWorkSpaceDataFilePathById', (id: string) => getWorkSpaceDataFilePathById(id))
